<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 4.2.&nbsp; Operator Concepts</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-4-SECT-2"></a>
<h3 id="631368-920" class="docSection1Title">4.2. Operator Concepts</h3>
<p class="docText">PHP has many types of operators. The categories<a name="IDX-CHP-4-0117"></a> 
 are:</p>
<ul><li><p class="docList">Arithmetic operators</p></li><li><p class="docList">Array operators</p></li><li><p class="docList">Assignment operators</p></LI><li><p class="docList">Bitwise operators</p></LI><LI><p class="docList">Comparison operators</p></LI><li><p class="docList">Execution operators</p></li><LI><p class="docList">Incrementing/decrementing operators</p></li><LI><p class="docList">Logical operators</p></li><LI><p class="docList">String operators</p></li></ul>
<p class="docText">The operators are listed as found on <a class="docLink" target="_blank" href="http://www.zend.com/manual/language.operators.php">http://www.zend.com/manual/language.operators.php</a>. There are some operators we're not going to discuss in order for you to get up and running with PHP as quickly as possible. These include some of the casting operators that we'll just skim the surface of, for now. When working with operators, there are four aspects that are critical:</p>
<UL><LI><p class="docList">Number of operands</p></li><li><p class="docList">Type of operands</p></LI><li><p class="docList">Order of precedence</P></li><LI><p class="docList">Operator associativity</P></LI></ul>
<p class="docText">The easiest place to start is by talking about the operands.</p>
<a name="learnphpmysql-CHP-4-SECT-2.1"></a>
<h4 id="title-IDAIJDFF" class="docSection2Title">4.2.1. Number of Operands</h4>
<a name="IDX-CHP-4-0118"></a> 
<a name="IDX-CHP-4-0119"></a> 
<a name="IDX-CHP-4-0120"></a> 

<p class="docText">Depending on which operator you are using, it may take different numbers of operands. Many operators are used to combine two expressions into a more complex single expression; these are called <span class="docEmphasis">binary<a name="IDX-CHP-4-0121"></a> 
 operators</span>.<a name="IDX-CHP-4-0122"></a> 
 Binary operators include addition, subtraction, multiplication, and division.</p>
<p class="docText">Other operators take only one operand; these are called <span class="docEmphasis">unary<a name="IDX-CHP-4-0123"></a> 
 operators</span>. Think of the negation operator (<tt>-</tt>) that multiplies a numeric value by -1. The auto-increment and -decrement operators described in <a class="docLink" href="learnphpmysql-CHP-3.html#learnphpmysql-CHP-3">Chapter 3</a> are also unary operators.<a name="IDX-CHP-4-0124"></a> 
</p>
<p class="docText">A <span class="docEmphasis">ternary<a name="IDX-CHP-4-0125"></a> 
 operator</span> takes three operands. The shorthand for an <tt>if</tt> statement, which we'll talk about later when discussing conditionals, takes three operands.</p>

<a name="learnphpmysql-CHP-4-SECT-2.2"></a>
<h4 id="title-IDASLDFF" class="docSection2Title">4.2.2. Types of Operands</h4>
<a name="IDX-CHP-4-0126"></a> 

<p class="docText">You need to be mindful of the type of operand an operator is meant to work on, because certain operators expect their operands to be particular data types. PHP attempts to make your life as easy as possible by automatically converting operands to the data type that an operator is expecting. But there are times that an automatic conversion isn't possible.</p>
<p class="docText">Mathematical operators are an example of where you need to be careful with your types. They take only numbers as operands. For example, when you try to multiply two strings, PHP can convert the strings to numbers. While <tt>"Becker" * "Furniture"</tt> is not a valid expression, it returns zero. Because the strings don't contain simple numbers, an expression that is converted without an error is <tt>"70" * "80"</tt>. This outputs to 5600. Although 70 and 80 are strings, PHP is able to convert them to the number type required by the mathematical operator.</p>
<p class="docText">There will be times when you want to explicitly set or convert a variable's type. There are two ways to do this in PHPfirst, by using <tt>settype</tt> to actually change the data type, or second, by casting, which temporarily converts the value. PHP uses <span class="docEmphasis">casting</span> to convert data types. When PHP does the casting for you automatically, it's called <span class="docEmphasis">implicit casting</span>.<a name="IDX-CHP-4-0127"></a> 
 You can also specify data types explicitly, but it's not something that you'll likely need to do.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><TD valign="top">
<p class="docText">PHP uses implicit casting to the type that the operator requires.</p>
</TD></tr></table></td></tr></table></P><br>
<p class="docText">The cast types allowed are:</p>
<dl class="docList">
<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">(int), (integer)</span></span></span></p></dt>
<dd><p class="docList">Cast to integer</P></dd>

<dt><br><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">(bool), (boolean)</span></span></span></p></dt>
<dd><p class="docList">Cast to Boolean</p></dd>

<dt><BR><P><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">(float), (double), (real)</span></span></span></p></dt>
<dd><p class="docList">Cast to float</p></dd>

<dt><br><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">(string)</span></span></span></P></dt>
<dd><p class="docList">Cast to string</p></dd>

<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">(array)</span></span></span></P></dt>
<dd><p class="docList">Cast to array</P></dd>

<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">(object)</span></span></span></p></dt>
<dd><p class="docList">Cast to object</p></dd>
</dl>
<p class="docText">To use a cast, place it before the variable to cast, as in <a class="docLink" href="#learnphpmysql-CHP-4-EX-2">Example 4-2</a>. The <tt>$test_string</tt> variable contains the string "1234".</p>
<a name="learnphpmysql-CHP-4-EX-2"></a><h5 id="title-IDA5ODFF" class="docExampleTitle">Example 4-2. Casting a variable</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
$test=1234;
$test_string=(string)$test;
</pre><br>

</td></tr></table></p>
<p class="docText">Keep in mind that it may not always be obvious what will happen when casting between certain types. You might run into problems if you don't watch yourself when manipulating variable types.</p>
<p class="docText">Some binary operators such as the <span class="docEmphasis">assignment<a name="IDX-CHP-4-0128"></a> 
 operators</span><a name="IDX-CHP-4-0129"></a> 
 have further restrictions on the lefthand operand. Because the assignment operator is assigning a value to the lefthand operator, it must be something that can take a value such as a variable. <a class="docLink" href="#learnphpmysql-CHP-4-EX-3">Example 4-3</a> demonstrates good and bad lefthand expressions.</P>
<a name="learnphpmysql-CHP-4-EX-3"></a><h5 id="title-IDA3PDFF" class="docExampleTitle">Example 4-3. Lefthand expressions</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
3 = $locations; // bad - a value can not be assign to the literal 3
$a + $b = $c; //bad - the expression on the left isn't one variable
$c = $a + $b; //OK
$stores = "Becker"." "."Furniture"; // OK
</pre><BR>

</td></tr></table></p>
<P><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText">There is a simpler way to remember this. The lefthand expression in assignment operations is known as an <span class="docEmphasis">L-value</span>. L-values in PHP are variables, elements of an array, and object properties. Don't worry about object properties.</p>
</td></tr></table></td></TR></table></P><br>
<a name="learnphpmysql-CHP-4-SECT-2.2.1"></a>
<h5 id="title-IDATQDFF" class="docSection3Title">4.2.2.1. Order of precedence</h5>
<a name="IDX-CHP-4-0130"></a> 
<a name="IDX-CHP-4-0131"></a> 

<p class="docText">The <span class="docEmphasis">order of precedence</span><a name="IDX-CHP-4-0132"></a> 
 of an operator determines which operator processes first in an expression. For instance, the multiplication and division process before addition and subtraction. You can see a simplified table at <a class="docLink" target="_blank" href="http://www.zend.com/manual/language.operators.php#language.operators.precedence">http://www.zend.com/manual/language.operators.php#language.operators.precedence</a>.</p>
<p class="docText">If the operators have the same precedence, they are processed in the order they appear in the expression. For example, multiplication and division process in the order they appear in an expression, because they have the same precedence. Operators with the same precedence can occur in any order without affecting the result.</P>
<p class="docText">Most expressions do not have more than one operator of the same precedence level, or the order in which they process doesn't change the result. As shown in <a class="docLink" href="#learnphpmysql-CHP-4-EX-4">Example 4-4</a>, when adding and subtracting, it doesn't matter whether you add or subtract firstthe result is still 1.</p>
<a name="learnphpmysql-CHP-4-EX-4"></a><H5 id="title-IDAYRDFF" class="docExampleTitle">Example 4-4. Order of precedence</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><TD>

<pre>
2 + 4 - 5  == 1;
4 - 5 + 2 == 1;

4 * 5 / 2 == 10;
5 / 2 * 4 == 10;

2 + 4 - 5  == 1;
4 - 5 + 2 == 1;

4 * 5 / 2 == 10;
5 / 2 * 4 == 10;
</pre><br>

</td></tr></table></p>
<p class="docText">When using expressions that contain operators of different precedence levels, the order can change the value of the expression. You can use parentheses, <tt>(</tt> and <tt>)</tt>, to override the precedence levels or just to make the expression easier to read. <a class="docLink" href="#learnphpmysql-CHP-4-EX-5">Example 4-5</a> shows how to change the default precedence.</p>
<a name="learnphpmysql-CHP-4-EX-5"></a><h5 id="title-IDANSDFF" class="docExampleTitle">Example 4-5. The multiplication is done last because of the override</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
echo 2 * 3 + 4 + 1;
11
echo 2 * (3 + 4 + 1);
16
</pre><br>

</td></tr></table></p>
<p class="docText">PHP has several levels of precedence, enough so that it's difficult to keep track of them without checking a reference. <a class="docLink" href="#learnphpmysql-CHP-4-TABLE-2">Table 4-2</a> is a list of operators in PHP sorted by order of precedence from highest to lowest. Operators with the same level number are all of the same precedence.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><td valign="top">
<p class="docText">The Association column lists operators that are right to left instead of left to right. We'll discuss associativity next.</p>
</td></TR></table></td></tr></table></P><br>
<a name="learnphpmysql-CHP-4-TABLE-2"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><H5 class="docTableTitle">Table 4-2. List of PHP operators</h5></caption><colgroup span="5"><col><col><col><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Operator</p></th><th class="thead" scope="col" align="left"><p class="docText">Description</p></th><th class="thead" scope="col" align="left"><p class="docText">Operands</P></th><th class="thead" scope="col" align="left"><p class="docText">Association</P></th><th class="thead" scope="col" align="left"><p class="docText">Level</p></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText">NEW</P></td><TD class="docTableCell" align="left"><p class="docText">Create new object</p></TD><TD class="docTableCell" align="left"><p class="docText">Constructor call</P></td><td class="docTableCell" align="left"><p class="docText">Right to left</p></td><td class="docTableCell" align="left"><p class="docText">1</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">.</p></td><td class="docTableCell" align="left"><p class="docText">Property access (dot notation)</p></TD><td class="docTableCell" align="left"><p class="docText">Objects</p></TD><TD class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">2</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">[ ]</p></TD><td class="docTableCell" align="left"><p class="docText">Array index</p></TD><td class="docTableCell" align="left"><p class="docText">Array, integer, or string</p></td><td class="docTableCell">&nbsp;</TD><TD class="docTableCell" align="left"><p class="docText">2</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">()</p></TD><td class="docTableCell" align="left"><p class="docText">Function call</P></TD><TD class="docTableCell" align="left"><p class="docText">Function or argument</p></td><td class="docTableCell">&nbsp;</td><td class="docTableCell" align="left"><p class="docText">2</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">!</p></td><td class="docTableCell" align="left"><p class="docText">Logical NOT</p></TD><td class="docTableCell" align="left"><p class="docText">Unary</p></TD><TD class="docTableCell" align="left"><p class="docText">Right to left</p></TD><td class="docTableCell" align="left"><p class="docText">3</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText">~</P></td><td class="docTableCell" align="left"><p class="docText">Bitwise NOT</P></td><td class="docTableCell" align="left"><p class="docText">Unary</p></td><TD class="docTableCell" align="left"><p class="docText">Right to left</P></td><td class="docTableCell" align="left"><p class="docText">3</p></td></TR><tr><TD class="docTableCell" align="left"><p class="docText"><tt>++, --</tt></p></TD><TD class="docTableCell" align="left"><p class="docText">Increment and decrement operators</P></td><td class="docTableCell" align="left"><p class="docText">1value</p></td><td class="docTableCell" align="left"><p class="docText">Right to left</p></td><td class="docTableCell" align="left"><p class="docText">3</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>+, -</tt></p></TD><td class="docTableCell" align="left"><p class="docText">Unary plus, negation</p></TD><TD class="docTableCell" align="left"><p class="docText">Number</p></TD><td class="docTableCell" align="left"><p class="docText">Right to left</p></td><TD class="docTableCell" align="left"><p class="docText">3</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText">(int)</P></td><td class="docTableCell" align="left"><p class="docText">Cast operators</p></td><TD class="docTableCell" align="left"><p class="docText">Unary</P></td><td class="docTableCell" align="left"><p class="docText">Right to left</p></td><TD class="docTableCell" align="left"><p class="docText">3</p></TD></tr><TR><TD class="docTableCell" align="left"><p class="docText">(double)</P></td><td class="docTableCell" align="left"><p class="docText">Cast operators</p></td><td class="docTableCell" align="left"><p class="docText">Unary</p></td><td class="docTableCell" align="left"><p class="docText">Right to left</p></td><td class="docTableCell" align="left"><p class="docText">3</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">(string)</p></TD><TD class="docTableCell" align="left"><p class="docText">Cast operators</p></TD><td class="docTableCell" align="left"><p class="docText">Unary</p></td><TD class="docTableCell" align="left"><p class="docText">Right to left</p></td><TD class="docTableCell" align="left"><p class="docText">3</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText">(array)</p></td><TD class="docTableCell" align="left"><p class="docText">Cast operators</P></td><td class="docTableCell" align="left"><p class="docText">Unary</p></td><TD class="docTableCell" align="left"><p class="docText">Right to left</p></TD><td class="docTableCell" align="left"><p class="docText">3</P></TD></TR><tr><td class="docTableCell" align="left"><p class="docText">(object)</p></td><td class="docTableCell" align="left"><p class="docText">Cast operators</p></td><td class="docTableCell" align="left"><p class="docText">Unary</p></td><td class="docTableCell" align="left"><p class="docText">Right to left</p></td><td class="docTableCell" align="left"><p class="docText">3</P></td></tr><TR><TD class="docTableCell" align="left"><p class="docText">@</p></TD><td class="docTableCell" align="left"><p class="docText">Inhibit errors</p></td><TD class="docTableCell" align="left"><p class="docText">Unary</p></td><TD class="docTableCell" align="left"><p class="docText">Right to left</p></td><TD class="docTableCell" align="left"><p class="docText">3</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText"><tt>*, /, %</tt></P></td><td class="docTableCell" align="left"><p class="docText">Multiplication, division</p></td><TD class="docTableCell" align="left"><p class="docText">Numbers</p></TD><td class="docTableCell">&nbsp;</TD><TD class="docTableCell" align="left"><p class="docText">4</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>+, -</tt></p></td><td class="docTableCell" align="left"><p class="docText">Addition, subtraction</p></td><td class="docTableCell" align="left"><p class="docText">Numbers</p></td><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">5</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">.</P></td><td class="docTableCell" align="left"><p class="docText">Concatenation</p></TD><td class="docTableCell" align="left"><p class="docText">Strings</p></TD><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">5</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText"><tt>&lt;&lt;, &gt;&gt;</tt></P></td><td class="docTableCell" align="left"><p class="docText">Bitwise shift left, bitwise shift right</p></td><TD class="docTableCell" align="left"><p class="docText">Binary</p></TD><td class="docTableCell">&nbsp;</TD><TD class="docTableCell" align="left"><p class="docText">6</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>&lt;, &lt;=, &gt;, &gt;=</tt></p></td><td class="docTableCell" align="left"><p class="docText">Comparison operators</p></td><td class="docTableCell" align="left"><p class="docText">Numbers, strings</p></td><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">7</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText"><tt>==, !=</tt></P></td><td class="docTableCell" align="left"><p class="docText">Equality, inequality</p></TD><td class="docTableCell" align="left"><p class="docText">Any</p></TD><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">8</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText"><tt>===, !==</tt></P></td><td class="docTableCell" align="left"><p class="docText">Identity, non-identity</p></td><TD class="docTableCell" align="left"><p class="docText">Any</p></TD><td class="docTableCell">&nbsp;</TD><TD class="docTableCell" align="left"><p class="docText">8</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">&amp;</p></td><td class="docTableCell" align="left"><p class="docText">Bitwise AND</p></td><td class="docTableCell" align="left"><p class="docText">Binary</p></td><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">9</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">^</P></td><td class="docTableCell" align="left"><p class="docText">Bitwise NOR</p></TD><td class="docTableCell" align="left"><p class="docText">Binary</p></TD><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">10</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">|</P></td><td class="docTableCell" align="left"><p class="docText">Bitwise OR</p></td><TD class="docTableCell" align="left"><p class="docText">Binary</p></TD><td class="docTableCell">&nbsp;</TD><TD class="docTableCell" align="left"><p class="docText">11</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">&amp;&amp;</p></td><td class="docTableCell" align="left"><p class="docText">Logical AND</p></td><td class="docTableCell" align="left"><p class="docText">Boolean</p></td><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">12</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">||</P></td><td class="docTableCell" align="left"><p class="docText">Logical OR</p></TD><td class="docTableCell" align="left"><p class="docText">Boolean</p></TD><td class="docTableCell">&nbsp;</td><TD class="docTableCell" align="left"><p class="docText">13</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">? :</P></td><td class="docTableCell" align="left"><p class="docText">Conditional</p></td><TD class="docTableCell" align="left"><p class="docText">Boolean</p></TD><td class="docTableCell" align="left"><p class="docText">Right to left</P></TD><TD class="docTableCell" align="left"><p class="docText">14</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">=</p></td><td class="docTableCell" align="left"><p class="docText">Assignment</p></td><td class="docTableCell" align="left"><p class="docText">1value=any</p></td><td class="docTableCell" align="left"><p class="docText">Right to left</P></td><td class="docTableCell" align="left"><p class="docText">15</P></TD></tr><TR><td class="docTableCell" align="left"><p class="docText">AND</p></td><TD class="docTableCell" align="left"><p class="docText">Logical AND</p></td><TD class="docTableCell" align="left"><p class="docText">Boolean</p></td><TD class="docTableCell">&nbsp;</td><td class="docTableCell" align="left"><p class="docText">16</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">OR</p></td><td class="docTableCell" align="left"><p class="docText">Logical OR</P></td><TD class="docTableCell" align="left"><p class="docText">Boolean</p></TD><TD class="docTableCell">&nbsp;</TD><td class="docTableCell" align="left"><p class="docText">17</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">XOR</p></td><td class="docTableCell" align="left"><p class="docText">Logical XOR</p></td><td class="docTableCell" align="left"><p class="docText">Boolean</p></td><TD class="docTableCell">&nbsp;</td><td class="docTableCell" align="left"><p class="docText">18</P></TD></tr></table></P><br>

<a name="learnphpmysql-CHP-4-SECT-2.2.2"></a>
<h5 id="title-IDA0KEFF" class="docSection3Title">4.2.2.2. Associativity</h5>
<a name="IDX-CHP-4-0133"></a> 
<a name="IDX-CHP-4-0134"></a> 
<a name="IDX-CHP-4-0135"></a> 

<p class="docText">All operators process their operators in a certain direction. This direction is called <span class="docEmphasis">associativity</span>,<a name="IDX-CHP-4-0136"></a> 
 and it depends on the type of operator. Most operators are processed from left to right, which is called left associativity. For example, in the expression 3 + 5 - 2, 3 and 5 are added together, and then 2 is subtracted from the addition, resulting in 8. Left associativity means that the expression is evaluated from left to right. Right associativity means the opposite.</P>
<p class="docText">Since it has right associativity, the assignment operator is one of the exceptions, since it has right associativity. The expression <tt>$a=$b=$c</tt> processes by <tt>$b</tt> being assigned the value of <tt>$c</tt>, and then <tt>$a</tt> being assigned the value of <tt>$b</tt>. This assigns the same value to all of the variables. If the assignment operator is right associative, the variables might not have the same value.</p>
<p class="docText">If you're thinking that this is incredibly complicated, don't worry. These rules are only enforced if you fail to be explicit about your instructions. Keep in mind that you should always use brackets in your expressions to make your actual meaning very clear. This helps both PHP and also other people who may need to read your code.</p>


<a name="learnphpmysql-CHP-4-SECT-2.3"></a>
<H4 id="title-IDAIMEFF" class="docSection2Title">4.2.3. Relational Operators</h4>
<p class="docText">In <a class="docLink" href="learnphpmysql-CHP-3.html#learnphpmysql-CHP-3">Chapter 3</a>, we discussed assignment and math operators. Relational operators provide the ability to compare two operands and return either <tt>trUE</tt> or <tt>FALSE</tt> regarding the comparison. An expression that returns only <tt>TRUE</tt> or <tt>FALSE</tt> is called a Boolean expression, which we discussed earlier in this chapter. These comparisons include tests for equality<a name="IDX-CHP-4-0137"></a> 
 and less than or greater than. These comparison operators allow you to tell PHP when to do something based on a comparison being true so decisions can be made in your code.</p>
<a name="learnphpmysql-CHP-4-SECT-2.3.1"></a>
<h5 id="title-IDAENEFF" class="docSection3Title">4.2.3.1. Equality</h5>
<p class="docText">The equality operator,<a name="IDX-CHP-4-0138"></a> 
 a double equals sign (<tt>==</tt>), is used frequently. Using the single <tt>=</tt> in its place is a common logical error in programs, since it assigns values rather than tests equality.</p>
<p class="docText">If the two operands are equal, <tt>TRUE</tt> is returned; otherwise, <tt>FALSE</tt> is returned. If you're echoing your results, <tt>TRUE</tt> is printed as 1 in your browser. <tt>FALSE</tt> is 0, which won't display in your browser.</p>
<p class="docText">It's a simple construct but it also allows you to test for conditions. If the operands are of different types, PHP attempts to convert them before comparing.</p>
<p class="docText">For example, <tt>'1' == 1</tt> is true. Also, <tt>$a == 1</tt> is true if the variable <tt>$a</tt> is assigned to 1.</p>
<p class="docText">If you don't want the equals operator to automatically convert types, you can use the <span class="docEmphasis">identity operator</span>, a triple equals sign <tt>===</tt>, which checks that the values and types are the same. For example, <tt>'1' === 1</tt> is false because they're different types, since a string doesn't equal an integer.</p>
<p class="docText">Sometimes you might want to check to see whether two things are different. The <span class="docEmphasis">inequality operator</span>, an exclamation mark before the equals sign (<tt>!=</tt>), checks for the opposite of equality, which means not equal to.</P>
<pre>
'1' != 'A'    // true
'1' != '1'    // false
</pre><br>


<a name="learnphpmysql-CHP-4-SECT-2.3.2"></a>
<H5 id="title-IDA4OEFF" class="docSection3Title">4.2.3.2. Comparison operators</h5>
<a name="IDX-CHP-4-0139"></a> 
<a name="IDX-CHP-4-0140"></a> 

<p class="docText">You may need to check for more than just equality. <span class="docEmphasis">Comparison operators</span> test the relationship between two values. You may be familiar with these from high school math. They include less than (<tt>&lt;</tt>), less than or equal to (<tt>&lt;=</tt>), greater than (<tt>&gt;</tt>), and greater than or equal to (<tt>&gt;=</tt>).</P>
<p class="docText">For example, <tt>3&lt;4</tt> is <tt>TRUE</tt>, while <tt>3&lt;3</tt> is <tt>FALSE</tt> but <tt>3&lt;=3</tt> is <tt>TRUE</tt>.</p>
<p class="docText">Comparison operators are often used to check for something happening up until a set point. For example, a web store might offer free shipping if you purchase five or more items. So the code must compare the number of items to the number five before changing the shipping cost.</p>

<a name="learnphpmysql-CHP-4-SECT-2.3.3"></a>
<h5 id="title-IDAPQEFF" class="docSection3Title">4.2.3.3. Logical operators</h5>
<p class="docText">Logical operators work with the Boolean results of relational operators to build more complex logical<a name="IDX-CHP-4-0141"></a> 
 expressions; there are four logical operators<a name="IDX-CHP-4-0142"></a> 
 shown in <a class="docLink" href="#learnphpmysql-CHP-4-TABLE-3">Table 4-3</a>.</p>
<a name="learnphpmysql-CHP-4-TABLE-3"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 4-3. Logical operators</h5></caption><colgroup span="2"><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Logical operator</p></th><th class="thead" scope="col" align="left"><p class="docText">Meaning</p></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText">AND</P></td><td class="docTableCell" align="left"><p class="docText"><tt>TRUE</tt> if both operands must be <tt>TRUE</tt></p></TD></tr><tr><td class="docTableCell" align="left"><p class="docText">OR</P></td><td class="docTableCell" align="left"><p class="docText"><tt>TRUE</tt> if at least one operand is <tt>trUE</tt></p></TD></tr><tr><td class="docTableCell" align="left"><p class="docText">XOR</p></TD><TD class="docTableCell" align="left"><p class="docText"><tt>trUE</tt> if only one operand is <tt>trUE</tt></p></td></TR><tr><TD class="docTableCell" align="left"><p class="docText">NOT</p></TD><TD class="docTableCell" align="left"><p class="docText"><tt>TRUE</tt> if <tt>FALSE, FALSE</tt> if <tt>trUE</tt></p></td></tr></table></p><br>
<p class="docText">To test whether both operands are true, use the <tt>AND</tt> operator, also represented as double ampersands (<tt>&amp;&amp;</tt>). <tt>trUE</tt> is returned only if both operands are <tt>trUE</tt>; otherwise, <tt>FALSE</tt> is returned. See <a class="docLink" href="#learnphpmysql-CHP-4-TABLE-3">Table 4-3</a> for more information.</p>
<p class="docText">To test whether one operand is <tt>trUE</tt>, use the <tt>OR</tt> operator, which is also represented as double vertical bars (<tt>||</tt>). <tt>trUE</tt> is returned only if either or both operands are <tt>trUE</tt>.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><TD width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><td valign="top">
<p class="docText">Using the <tt>OR</tt> operator can create tricky program logic problems. If PHP finds that the first operand is <tt>TRUE</tt>, it won't evaluate the second operand. While this saves execution time, you need to be careful that the second operator doesn't contain code that needs to be executed for your program to work properly.</p>
</td></tr></table></TD></tr></table></p><BR>
<p class="docText">To test whether either operand is <tt>trUE</tt> <span class="docEmphasis">but not both</span>, use <tt>XOR. XOR</tt> returns <tt>trUE</tt> if one and only one operand is <tt>TRUE</tt>.</p>
<p class="docText">To negate a Boolean value, use the <tt>NOT</tt> operator represented as an exclamation point (<tt>!</tt>). It returns <tt>trUE</tt> if the operand has a value of <tt>FALSE</tt>. It returns <tt>FALSE</tt> if the operand is <tt>trUE</tt>.</p>
<P><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText">If you accidentally use <tt>&amp;</tt> instead of <tt>&amp;&amp;</tt> or <tt>|</tt> instead of <tt>||</tt>, you'll end up getting the wrong operator. They compare binary data bit by bit. PHP converts your operands into binary data and applies the binary operators.</p>
</TD></tr></table></TD></TR></table></P><br>
<p class="docText">Because they have different precedence levels, <tt>AND</tt> and <tt>OR</tt> have two representations. <a class="docLink" href="#learnphpmysql-CHP-4-TABLE-4">Table 4-4</a> displays logical statements and their results.</p>
<a name="learnphpmysql-CHP-4-TABLE-4"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 4-4. Logical statements and their results</h5></caption><colgroup span="2"><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Example logical statement</p></th><th class="thead" scope="col" align="left"><p class="docText">Result</p></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText">TRUE AND TRUE</p></td><td class="docTableCell" align="left"><p class="docText">TRUE</P></td></tr><TR><TD class="docTableCell" align="left"><p class="docText">FALSE AND TRUE</p></TD><td class="docTableCell" align="left"><p class="docText">FALSE</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText">TRUE OR FALSE</P></td><td class="docTableCell" align="left"><p class="docText">TRUE</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">FALSE OR FALSE</P></TD><td class="docTableCell" align="left"><p class="docText">FALSE</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">TRUE XOR TRUE</P></td><TD class="docTableCell" align="left"><p class="docText">FALSE</P></TD></tr><tr><td class="docTableCell" align="left"><p class="docText">!TRUE</p></td><td class="docTableCell" align="left"><p class="docText">FALSE</p></td></tr></table></p><br>



</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
